//
//  HMTabBarController.swift
//  Weibo
//
//  Created by xiongminghua on 16/7/18.
//  Copyright © 2016年 熊明华. All rights reserved.
//

import UIKit

class HMTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 添加子控制器
       self.addchildviewcontrollers()
        
        // 设置自定义tabBar
       // self.tabBar = HMTabBar()
        
        let mainTabBar = HMTabBar()
        
        //因为tabBar是只读属性，所以只能通过KVC赋值
        self.setValue(mainTabBar, forKey: "tabBar")
        
        // 定义和传递闭包
        mainTabBar.composeCallBack = { () -> Void in
            
            print("调用了")
            
            // TODO: 进行modal跳转
        }
        
    }
    
    
    /**
        添加所有子控制器
     */
    func addchildviewcontrollers() {
        
        // 首页
        let homeVC = HMHomeTableViewController()
        
        self.addChildViewController(homeVC, title: "首页", imgageName: "tabbar_home")
        
        // 信息
        let messageVC = HMMessageTableViewController()
        self.addChildViewController(messageVC, title: "信息", imgageName: "tabbar_message_center")
        
        //发现
        let discoverVC = HMDiscoverTableViewController()
        self.addChildViewController(discoverVC, title: "发现", imgageName: "tabbar_discover")
        
        //我
        let profileVC = HMProfileTableViewController()
        
        self.addChildViewController(profileVC, title: "我", imgageName: "tabbar_profile")
    }
     
    
    
    /**
    *  添加子控制器
    */
    func addChildViewController(controller: UIViewController, title: String, imgageName: String) {
        
        // 设置导航控制器
        let homeNav = UINavigationController(rootViewController: controller)
        
        // 设置标题
        controller.title = title
        // 设置tabBar图片
        controller.tabBarItem.image = UIImage(named: imgageName)
        
        // 设置tabBar标题的字体颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        let selectedName = imgageName + "_selected"
        // 设置选中时tabBar的图片 , 并且不需要渲染
        controller.tabBarItem.selectedImage = UIImage(named: selectedName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        self.addChildViewController(homeNav)
        
    }

   

}
